package com.atguigu.yygh.hosp.controller;

import com.atguigu.yygh.common.result.Result;
import com.atguigu.yygh.common.utils.MD5;
import com.atguigu.yygh.hosp.service.HospitalSetService;
import com.atguigu.yygh.model.hosp.HospitalSet;
import com.atguigu.yygh.vo.hosp.HospitalSetQueryVo;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import io.swagger.annotations.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Random;

@Api(tags = "医院设置管理")
@RestController
@RequestMapping("/admin/hosp/hospitalSet")
//@CrossOrigin  //允许跨域，前端可以收到来自不同ip或端口的数据，无需其他配置
public class HospitalSetController {

    //注入service
    @Autowired
    private HospitalSetService hospitalSetService;

    //1 查询医院设置表所有信息
    @ApiOperation(value = "获取所有医院设置")
    @GetMapping("findAll")
    public Result findAllHospitalSet(){
        //调用service
        List<HospitalSet> list = hospitalSetService.list();
        return Result.ok(list);
    }

    //2 逻辑删除医院设置
    @ApiOperation(value = "逻辑删除医院设置")
    @ApiParam(name = "id",value = "医院设置id",required = true)  //单个参数描述
    @DeleteMapping("{id}")  //无法在浏览器中直接访问DeleteMapping
    public Result removeHospitalSet(@PathVariable Long id){
        boolean flag = hospitalSetService.removeById(id);
        if(flag){
            return Result.ok();
        }else {
            return Result.fail();
        }
    }

    //3 条件查询带分页
    @ApiOperation(value = "分页带条件查询医院设置")
    /*@ApiImplicitParams(  //多参数说明
            {
                    @ApiImplicitParam(name = "current", value = "当前页", dataType = "Long"),  //单个参数描述
                    @ApiImplicitParam(name = "limit", value = "一页总条数", dataType = "Long"),  //单个参数描述
            }
             )*/
    @PostMapping("findPageHospSet/{current}/{limit}")  //当前页，每页记录数
    public Result findPageHospSet(@PathVariable long current,
                                  @PathVariable long limit,
                                  @RequestBody(required = false) HospitalSetQueryVo hospitalSetQueryVo){
        //只有@PostMapping才可以获取@RequestBody（json格式）,没加之前获取的就是一个一个的值，可以在swagger页面看到

        //创建page对象
        Page<HospitalSet> page = new Page<>(current,limit);
        //构造条件,这里的医院名称和编号前端不是必填的，所以可能有也可能没有
        String hosname = hospitalSetQueryVo.getHosname();
        String hoscode = hospitalSetQueryVo.getHoscode();
        QueryWrapper<HospitalSet> queryWrapper = new QueryWrapper();
        if(!StringUtils.isEmpty(hosname)){  //是springframework.util.StringUtils的工具类，不要引错了
            queryWrapper.like("hosname", hospitalSetQueryVo.getHosname());
        }
        if(!StringUtils.isEmpty(hoscode)){  //是springframework.util.StringUtils的工具类，不要引错了
            queryWrapper.eq("hoscode", hospitalSetQueryVo.getHoscode());
        }
        //QueryWrapper<HospitalSet> eq = queryWrapper.like("hosname", hospitalSetQueryVo.getHosname()).eq("hoscode", hospitalSetQueryVo.getHoscode());
        //调用方法实现分页查询
        Page<HospitalSet> pageHospitalSet = hospitalSetService.page(page, queryWrapper);
        return Result.ok(pageHospitalSet);
    }

    //4 添加医院设置接口
    @ApiOperation("添加医院设置接口")
    @PostMapping("saveHospSet")
    public Result saveHospSet(@RequestBody HospitalSet hospitalSet){
        //签名密钥(MD5加密-(当前时间+随机值))和状态(1 使用，0 不能使用)需要单独设置
        //为什么前端不传时间也会自动有时间？并没有使用mp的默认填充，数据库表里设置了默认填充
        hospitalSet.setStatus(1);
        Random random = new Random();
        hospitalSet.setSignKey(MD5.encrypt(System.currentTimeMillis()+""+random.nextInt(1000)));
        boolean save = hospitalSetService.save(hospitalSet);
        if(save){
            return Result.ok();
        }else {
            return Result.fail();
        }
    }


    //5 根据id获取医院设置
    @ApiOperation("根据id获取医院设置")
    @ApiParam(name = "id",value = "查询的医院id",required = true)
    @GetMapping("getHospSet/{id}")
    public Result getHospSet(@PathVariable Long id){

        /*//异常自定义模拟，自定义的异常需要手动抛出
        try{
            int a = 1/0;
        }catch (Exception e){
            throw new YyghException("失败"，201);  //自己指定状态码
        }*/


        HospitalSet byId = hospitalSetService.getById(id);
            return Result.ok(byId);
    }

    //6 修改医院设置
    @ApiOperation("修改医院设置")
    @PostMapping("updateHospitalSet")
    public Result updateHospSet(@RequestBody HospitalSet hospitalSet){
        boolean flag = hospitalSetService.updateById(hospitalSet);
        if(flag) {
            return Result.ok();
        } else {
            return Result.fail();
        }
    }

    //7 批量删除医院设置  传入参数【6，8】，即传入一个json数组就是这里的Long集合
    @ApiOperation("批量删除医院设置")
    @DeleteMapping("batchRemove")
    public Result batchRemoveHospitalSet(@RequestBody List<Long> idList) {
        hospitalSetService.removeByIds(idList);
        return Result.ok();
    }

    //8 医院设置锁定和解锁
    @ApiOperation("医院设置锁定和解锁")
    @PutMapping("lockHospitalSet/{id}/{status}")  //put与post的区别在于幂等性，put是幂等的，修改一次生效，后续相同修改返回相同结果，而post则不具有幂等性
    public Result locaHospitalSet(@PathVariable Long id,@PathVariable Integer status){
        HospitalSet hospitalSet = hospitalSetService.getById(id);
        //设置状态
        hospitalSet.setStatus(status);
        //调用方法
        hospitalSetService.updateById(hospitalSet);
        return Result.ok();
    }


    //9 发送签名key（密钥），医院自身平台与本平台对接时需要此密钥
    @ApiOperation(value = "发送签名key（密钥")
    @PutMapping("sendKey/{id}")
    public Result sendHospitalSet(@PathVariable Long id){
        HospitalSet hospitalSet = hospitalSetService.getById(id);
        String signKey = hospitalSet.getSignKey();
        String hoscode = hospitalSet.getHoscode();
        //TODO 发送短信
        return Result.ok();

        //未实现
    }

}
